// pages/cart/cart.js
//  全局变量管理
import store from '../../store'
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    addressInfo: null,
    //
    carList: [],
    userInfo: null,
    carPrice: 0,
    carNum: 0,
    optAll: true,
    kongShow: 'none',
    hidden: true,
    show: false
  },
  //购物车点减少
  reduceNum: function (item) {
    let newItem = item.currentTarget.dataset.item
    let newCarList = this.data.carList
    for (let element of newCarList) {
      if (element.id === newItem.id) {
        element.num > 1 ? --element.num : 1
      }
    }
    // 计算一下总金额
    this.countPrice()
    this.setData({
      carList: newCarList
    })
  },
  //购物车点增加
  addNum: function (item) {
    let newItem = item.currentTarget.dataset.item
    let newCarList = this.data.carList
    for (let element of newCarList) {
      if (element.id === newItem.id) {
        ++element.num
      }
    }
    // 计算一下总金额
    this.countPrice()
    this.setData({
      carList: newCarList
    })

  },
  // 计算总金额
  countPrice: function () {
    let newCarList = this.data.carList
    let newPrice = 0
    let newNum = 0
    // 四舍五入 toFixed(2)
    for (let element of newCarList) {
      //  只有勾选了的才计算
      if (element.opt) {
        newPrice += (element.price * element.num)
        newNum += element.num
      }
    }
    this.setData({
      carPrice: newPrice.toFixed(2),
      carNum: newNum
    })
  },
  // 单个勾选情况
  optBtn: function (item) {
    let newItem = item.currentTarget.dataset.item
    let newCarList = this.data.carList
    // 单个勾选的时候，全选的动态
    let newOptAll = this.data.optAll, tempOpt = []
    for (let element of newCarList) {
      if (element.id === newItem.id) {
        element.opt = !element.opt
      }
      //把carList里面所有的opt组成一个数组
      tempOpt.push(element.opt)
    }
    // 判断如果没有false全选就钩上，否则就不勾
    if (tempOpt.indexOf(false) === -1){
      newOptAll = true
    }else{
      newOptAll = false
    }
    // 计算一下总金额
    this.countPrice()
    this.setData({
      carList: newCarList,
      optAll: newOptAll
    })
  },
  //底部全选
  optAllBtn: function () {
    let newCarList = this.data.carList
    let newOptAll = this.data.optAll
    // console.log(newOptAll)
    if (newOptAll) {
      for (let element of newCarList) {
        element.opt = false
      }
      newOptAll = false
    }else{
      for (let element of newCarList) {
        element.opt = true
      }
      newOptAll = true
    }
    // 计算一下总金额
    this.countPrice()
    this.setData({
      carList: newCarList,
      optAll: newOptAll
    })
  },
  // 编辑 删除购物车
  delCar: function () {
    let newCarList = this.data.carList
    for (var i = newCarList.length - 1; i >= 0; i--) {
      //如果是allopt = true，则从数组删掉
      if (newCarList[i].opt) {
        newCarList.splice(i, 1)
      }
    }
    if (newCarList.length < 1){
      this.data.optAll = false
      this.data.hidden = true
    }
    // 计算一下总金额
    this.countPrice()
    this.setData({
      carList: newCarList,
      optAll: this.data.optAll,
      hidden: this.data.hidden,
      show: !this.data.hidden
    })
  },
  
  // 编辑按钮 完成按钮
  editbtn: function (e) {
    this.setData({
      block: "inline-block",
      none: "none",
    })
  },
  editenterbtn: function (e) {
    this.setData({
      block: "none",
      none: "inline-block",
    })
  },
  // 去付款调取地址
  // 获取地址
  chooseAddress() {
    if(this.data.carNum){
      wx.chooseAddress({
        success: (res) => {
          console.log(res)
          this.setData({
            addressInfo: res
          })
        }
      })
    } else{
      
    }
   
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 如果有值就直接负值，如果没就再请求
    if (app.storeData.userInfo) {
      this.data.userInfo = app.storeData.userInfo
      this.data.hasUserInfo = true
      // this.update()
      // console.log(this.data.userInfo )
    } else if (this.data.canIUse) {
      app.userInfoReadyCallback = res => {
        this.data.userInfo = res.userInfo
        this.data.hasUserInfo = true
        // this.update()
        console.log('2')
        console.log(this.data.userInfo)
      }
    } else {
      wx.getUserInfo({
        success: res => {
          app.storeData.userInfo = res.userInfo
          this.data.userInfo = res.userInfo
          this.data.hasUserInfo = true
          // this.update()
          console.log('3')
          console.log(this.data.userInfo)
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // 页面进入动画效果
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   // 判断购物车数据为空的时候
    let len = app.storeData.carStore.length,
    show = this.data.show,
    hidden = this.data.hidden,
    newOptAll = this.data.optAll
    if (len) {
      newOptAll = true
      hidden = false
    } else {
      // console.log('a')
      newOptAll = false
      hidden = true
    }
     // 获取仓库购物车数据
    this.setData({
      carList: app.storeData.carStore,
      optAll: newOptAll,
      hidden: hidden,
      show: !hidden,
      block: "none",
      none: "inline-block",
    })
    this.countPrice()
    // this.allOptInit();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
})